<template>
	<view class="groupactivelist">
		<view class="groupactivelist_top_block">
			<view class="groupauditlist_tabs">
				<view @click="changeTabs(0)"
					:class="params.type==0?'groupauditlist_tabs_cell tabs_on':'groupauditlist_tabs_cell'">全部</view>
				<view @click="changeTabs(1)"
					:class="params.type==1?'groupauditlist_tabs_cell tabs_on':'groupauditlist_tabs_cell'">待审核</view>
				<view @click="changeTabs(2)"
					:class="params.type==2?'groupauditlist_tabs_cell tabs_on':'groupauditlist_tabs_cell'">未通过</view>
				<view @click="changeTabs(3)"
					:class="params.type==3?'groupauditlist_tabs_cell tabs_on':'groupauditlist_tabs_cell'">待发布</view>
				<view @click="changeTabs(4)"
					:class="params.type==4?'groupauditlist_tabs_cell tabs_on':'groupauditlist_tabs_cell'">已发布</view>
			</view>
		</view>
		<view v-for="(item,index) in list" :key="item.huodong_id" class="groupactivelist_cell">
			<view @click="goInfo(item.huodong_id)">
				<view class="info">
					<view v-if="item.huodong_status == 1" class="a_audit">待审核</view>
					<view v-if="item.huodong_status == 2" class="a_no">未通过</view>
					<view v-if="item.huodong_status == 3" class="a_publish">待发布</view>
					<view v-if="item.huodong_status == 4" class="a_recruit">招募中</view>
					<view v-if="item.huodong_status == 5" class="a_pending">进行中</view>
					<view v-if="item.huodong_status == 6" class="a_end">已结束</view>
					<view class="display_flex" style="justify-content: space-between">
						<view class="flex-1 ellipsis">活动名称：
							<text>{{item.title}}</text>
						</view>
					</view>
					<view class="mt-5">
						<view>活动日期：{{item.start_time?item.start_time.substring(0,10):''}} 至
							{{item.end_time?item.end_time.substring(0,10):''}}</view>
					</view>
				</view>
			</view>
			<view class="mt-10 display_flex" style="justify-content:space-between;">
				<view class="c-666666 f-12">
					<view v-if="item.huodong_status == 1||item.huodong_status == 2" style="text-decoration: underline;"
						@click="delActive(item.huodong_id,index)">删除活动</view>
				</view>
				<view class="display_flex">
					<view v-if="item.huodong_status == 6" class="group_public_btn_mini mr-10"
						@click="gohexiao(item.huodong_id,item.validate_status)">活动核销</view>
					<view v-if="item.huodong_status == 1||item.huodong_status == 2||item.huodong_status == 3"
						class="group_public_btn_mini" @click="goEdit(item.huodong_id)">编辑活动</view>
					<view v-else class="group_forbidden_btn_mini">编辑活动</view>
					<view v-if="item.huodong_status == 4||item.huodong_status == 5" class="group_public_btn_mini ml-10"
						@click="goAudit(item.huodong_id)">审核管理</view>
					<view v-if="item.huodong_status == 4" class="group_public_btn_mini ml-10"
						@click="goAuditOffline(item.huodong_id,index)">活动下线</view>
						<view v-if="item.huodong_status == 3" class="group_public_btn_mini ml-10"
							@click="goAuditPush(item.huodong_id,index)">活动发布</view>
				</view>
			</view>
		</view>
		<view class="button" @click="releaseActive">+发布活动</view>
		<view class="page_bottom_loading" :style="'padding-bottom:'+pdbottom+'rpx;'">{{loadingTitle}}</view>
	</view>
</template>

<script>
	import {
		getAdminGroupActiveList,
		noticeTemplate,
		delAdminGroupActive,
		submitGroupActivePush,
		submitGroupActiveOffline
	} from '@/api/motortime.js';
	let app = getApp()
	export default {
		data() {
			return {
				params: {
					club_id: '',
					type: 0,
					page: 1,
					size: 15,
				},
				list: [],
				loadingTitle: '',
				pdbottom: uni.getSystemInfoSync().safeAreaInsets.bottom ? uni.getSystemInfoSync().safeAreaInsets.bottom +
					140 : 140,
			};
		},
		onShow() {
			if (!uni.getStorageSync('access_token')) {
				uni.navigateTo({
					url: '/pages/login/login'
				})
			}
			if (uni.getStorageSync('userinfo').is_staff == 0) {
				uni.switchTab({
					url: '/pages/index/index'
				})
			} else {
				if (uni.getStorageSync('onload')) {
					this.params.page = 1
					this.list = []
					this.getAdminGroupActiveList()
					uni.removeStorage({
						key: 'onload'
					})
				}
			}
		},
		onLoad(options) {
			if (uni.getStorageSync('userinfo').is_staff != 0) {
				this.club_id = options.club_id
				this.params.club_id = options.club_id
				this.getAdminGroupActiveList()
			}
			var params = {
				event_code: '车友会活动管理',
				path: 'pages/groupmanage/groupactivelist',
				event_id: '',
				title: '',
				source_page: '',
				value: '',
			}
			app.BurialPoint(params)
		},
		methods: {
			getAdminGroupActiveList() {
				getAdminGroupActiveList(this.params).then(res => {
					if (res.state == 1) {
						this.list = [...this.list, ...res.data.data]
						this.current_page = res.data.current_page
						this.last_page = res.data.last_page
						if (this.list.length == 0) {
							this.loadingTitle = "- 暂无更多 -"
						}
					} else {
						uni.showToast({
							title: res.error[0],
							mask: true,
							icon: 'none',
							duration: 1500
						})
					}
				})
			},

			changeTabs(index) {
				this.params.type = index
				this.params.page = 1
				this.list = []
				this.getAdminGroupActiveList()
			},
			goInfo(id) {
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: id,
					title: '',
					source_page: '点击查看详情',
					value: '',
				}
				app.BurialPoint(params)
				uni.navigateTo({
					url: '/pages/groupmanage/groupactiveinfo?club_id=' + this.club_id + '&isCheck=1&huodong_id=' +
						id
				})
			},
			goEdit(id) {
				uni.navigateTo({
					url: '/pages/groupmanage/groupactivepost?club_id=' + this.club_id + '&huodong_id=' + id
				})
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: id,
					title: '',
					source_page: '点击编辑',
					value: '',
				}
				app.BurialPoint(params)
			},
			goAudit(id) {
				uni.navigateTo({
					url: '/pages/groupmanage/groupactiveauditlist?club_id=' + this.club_id + '&huodong_id=' + id
				})
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: id,
					title: '',
					source_page: '点击审核管理',
					value: '',
				}
				app.BurialPoint(params)
			},
			releaseActive() {
				var that = this
				// #ifdef  MP-WEIXIN
				uni.requestSubscribeMessage({
					tmplIds: ['RmPFHUx1IW1qdYz5iDyPVrsIoB0-jNG_UH4eajDcjGE'],
					success: function(res) {
						noticeTemplate({
							template_type: 6 //4:车友会；5:活动报名结果；6:活动创建结果
						}).then(res => {
							if (res.state == 1) {
								uni.navigateTo({
									url: '/pages/groupmanage/groupactivepost?club_id=' + that
										.club_id
								})
							} else {
								uni.showToast({
									title: res.error[0],
									mask: true,
									icon: 'none',
									duration: 1500
								})
							}
						})
					},
					fail: function(e) {
						console.log(e)
					}
				})
				// #endif
				// #ifndef  MP-WEIXIN
				uni.navigateTo({
					url: '/pages/groupmanage/groupactivepost?club_id=' + that.club_id
				})
				// #endif
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: '',
					title: '',
					source_page: '点击发布活动',
					value: '',
				}
				app.BurialPoint(params)
			},
			delActive(id, index) {
				var that = this
				uni.showModal({
					content: '确定要删除此活动吗？',
					confirmText: '删除',
					confirmColor: '#00A0DC',
					cancelText: "取消",
					success: function(res) {
						if (res.confirm) {
							delAdminGroupActive({
								huodong_id: id
							}).then(res => {
								if (res.state == 1) {
									uni.showToast({
										title: '删除成功',
										mask: true,
										icon: 'success',
										duration: 1500
									})
									that.list.splice(index, 1);
								} else {
									uni.showToast({
										title: res.error[0],
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: '',
					title: '',
					source_page: '删除活动',
					value: '',
				}
				app.BurialPoint(params)
			},
			goAuditOffline(id,index) {
				var that = this
				uni.showModal({
					content: '确定要下线此活动吗？',
					confirmText: '下线',
					confirmColor: '#00A0DC',
					cancelText: "取消",
					success: function(res) {
						if (res.confirm) {
							submitGroupActiveOffline({
								huodong_id: id
							}).then(res => {
								if (res.state == 1) {
									uni.showToast({
										title: '下线成功',
										mask: true,
										icon: 'success',
										duration: 1500
									})
									that.list[index].huodong_status = 3
								} else {
									uni.showToast({
										title: res.error[0],
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: '',
					title: '',
					source_page: '下线活动',
					value: '',
				}
				app.BurialPoint(params)
			},
			goAuditPush(id,index) {
				var that = this
				uni.showModal({
					content: '确定要发布此活动吗？',
					confirmText: '发布',
					confirmColor: '#00A0DC',
					cancelText: "取消",
					success: function(res) {
						if (res.confirm) {
							submitGroupActivePush({
								huodong_id: id
							}).then(res => {
								if (res.state == 1) {
									uni.showToast({
										title: '发布成功',
										mask: true,
										icon: 'success',
										duration: 1500
									})
									that.list[index].huodong_status = 4
								} else {
									uni.showToast({
										title: res.error[0],
										mask: true,
										icon: 'none',
										duration: 1500
									})
								}
							})

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: '',
					title: '',
					source_page: '发布活动',
					value: '',
				}
				app.BurialPoint(params)
			},
			gohexiao(huodong_id, validate_status) {
				if (validate_status == 0) {
					uni.navigateTo({
						url: '/pages/groupmanage/groupactiveverify?huodong_id=' + huodong_id
					})
				} else {
					uni.navigateTo({
						url: '/pages/groupmanage/groupactiveverifyresult?huodong_id=' + huodong_id
					})
				}
				var params = {
					event_code: '车友会活动管理',
					path: 'pages/groupmanage/groupactivelist',
					event_id: huodong_id,
					title: '',
					source_page: '点击核销',
					value: '',
				}
				app.BurialPoint(params)
			}
		},
		onPullDownRefresh() {
			this.params.page = 1
			this.list = []
			this.getAdminGroupActiveList()
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.current_page < this.last_page) {
				this.showLoading = true
				this.params.page = this.params.page + 1
				this.getAdminGroupActiveList()
			} else {
				this.showLoading = false
				if (this.list.length > 0) {
					this.loadingTitle = "- 已经到底了 -"
				} else {
					this.loadingTitle = "- 暂无更多 -"
				}

			}
		}
	}
</script>

<style lang="scss">
	.groupactivelist {
		padding: 0 30rpx 50rpx 30rpx;

		.groupactivelist_top_block {
			padding-top: 30rpx;
			position: sticky;
			top: 0;
			z-index: 1;
			background-color: #ffffff;

			.groupauditlist_tabs {
				height: 80rpx;
				font-size: 26rpx;
				display: flex;
				align-items: center;
				justify-content: space-around;
				border-radius: 8rpx;
				border: 2rpx solid #E9E9E9;

				.groupauditlist_tabs_cell {
					width: 20%;
					height: 26rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					border-right: 4rpx solid #000000;
				}

				.groupauditlist_tabs_cell:last-child {
					border-right: 0;
				}

				.tabs_on {
					color: #00A0DC;
				}
			}
		}

		.groupactivelist_cell {
			font-size: 28rpx;
			margin-top: 40rpx;
			padding: 20rpx;
			border-radius: 8rpx;
			border: 2rpx solid #F7F7F7;
			background-color: #F7F7F7;

			.info {
				background-color: #F0F0F0;
				border-radius: 8rpx;
				padding: 20rpx;
				font-size: 26rpx;
				position: relative;

				.huodong_status {
					width: 70rpx;
					height: 30rpx;
					position: absolute;
					right: 0;
					top: 0;
				}
			}

			.label {
				color: #999;
				padding-right: 10rpx;
			}
		}

		.mastbar {
			position: fixed;
			bottom: 200rpx;
			right: 30rpx;
			z-index: 1;

			.mastbar_post {
				width: 120rpx;
				height: 120rpx;
				background-color: #00A0DC;
				border-radius: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.release {
				width: 40rpx;
				height: 44rpx;
			}
		}

		.button {
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			background-color: #00A0DC;
			font-size: 30rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 140rpx;
		}
	}
</style>